﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using cbmobile.DAC;
using PQT.API;
using System.Data;
using cbmobile.BLC;
using System.Data.SqlClient;

namespace cbmobile.BLC
{
    public class BLC_SanPham
    {
        private CBMobileEntities da = null;
        DBConnection dbCn = new DBConnection("PQT");
        SqlConnection cnn = null;


        public BLC_SanPham()
        {
            da = new CBMobileEntities();
        }


        public IList<SanPham> All_Product()
        {
            return da.SanPhams.ToList();
        }

        public SanPham Get_SanPham_Memeber_byId(int Ma_SP)
        {
            IList<SanPham> list = da.SanPhams.Where(p => p.Ma_SP == Ma_SP).ToList();
            if (list.Count() == 0)
                return null;
            return list.First();
        }

        public DataTable Get_SP_Top()
        {
            DataTable dt = null;
            using (cnn = dbCn.InitConnection())
            {
                cnn.Open();
                SqlDataAdapter da = null;
                DataSet ds = new DataSet();
                SqlCommand cmdUp = new SqlCommand("pr_Get_SP_Top", cnn);
                cmdUp.CommandType = CommandType.StoredProcedure;
                try
                {
                    da = new SqlDataAdapter(cmdUp);
                    dt = new DataTable();
                    da.Fill(dt);
                }
                catch { }
                finally
                {
                    if (cnn.State == ConnectionState.Open)
                        cnn.Close();
                    cnn.Dispose();
                }

            }
            return dt;
        }


        public SanPham Get_SanPham_byId(int Ma_SP)
        {
            IList<SanPham> list = da.SanPhams.Where(p => p.Ma_SP == Ma_SP).ToList();
            if (list.Count() == 0)
                return null;
            return list.First();
        }


        public IList<SanPham> Rows_SanPham(int p_page, int p_pageSize, string p_name)
        {
            
            int preCount = (p_page - 1) * p_pageSize;
            IList<SanPham> list = da.SanPhams.Where(z => z.Ten_SP.Contains(p_name)).OrderByDescending(z => z.Ten_SP).Skip(preCount).Take(preCount + p_pageSize).ToList();
            return list;
        }

        public int GetTotalRecord(string p_name)
        {
            return da.SanPhams.Where(z => z.Ten_SP.Contains(p_name)).Count();
        }

        public void Delete(int Ma_SP)
        {
            var existing = da.SanPhams.Where(p => p.Ma_SP == Ma_SP).First();
            existing.TinhTrang = (int)CommonStatus.Delete;
            da.SaveChanges();
        }

        #region create
        public void Create(string Ten_SP,
                                string Size, string TrongLuong,
                                string HeDieuHanh, string CPU,
                                string OCung, string RAM, bool Support_3G,
                                string KetNoi3G, string Wifi, string Camera,
                                int TrangThaiKho, int TinhTrang,
                                string BaoHanh, string HangCongTy,
                                string PhuKien, string UuDai, int HangSX_ID,
                                string ThongSoKyThuat, string MoTaSanPham,
                                string HinhAnh, int Count_View, decimal GiaThiTruong, decimal GiaCongTy, DateTime NgayTao)
        {
            var objEnt = new SanPham();
            objEnt.Ten_SP = Ten_SP;
            objEnt.TrongLuong = TrongLuong;
            objEnt.HeDieuHanh = HeDieuHanh;
            objEnt.CPU = CPU;
            objEnt.OCung = OCung;
            objEnt.RAM = RAM;
            objEnt.Support_3G = Support_3G;
            objEnt.KetNoi3G = KetNoi3G;
            objEnt.Wifi = Wifi;
            objEnt.Camera = Camera;
            objEnt.TrangThaiKho = TrangThaiKho;
            objEnt.TinhTrang = TinhTrang;
            objEnt.BaoHanh = BaoHanh;
            objEnt.HangCongTy = HangCongTy;
            objEnt.PhuKien = PhuKien;
            objEnt.UuDai = UuDai;
            objEnt.HangSX_ID = HangSX_ID;
            objEnt.ThongSoKyThuat = ThongSoKyThuat;
            objEnt.MoTaSanPham = MoTaSanPham;
            objEnt.HinhAnh = HinhAnh;
            objEnt.Count_View = Count_View;
            objEnt.GiaThiTruong = GiaThiTruong;
            objEnt.GiaCongTy = GiaCongTy;
            objEnt.NgayTao = NgayTao;

            da.SanPhams.AddObject(objEnt);
            da.SaveChanges();
        }
        #endregion



        #region update
        public void Update(int Ma_SP, string Ten_SP,
                               string Size, string TrongLuong,
                               string HeDieuHanh, string CPU,
                               string OCung, string RAM, bool Support_3G,
                               string KetNoi3G, string Wifi, string Camera,
                               int TrangThaiKho, int TinhTrang,
                               string BaoHanh, string HangCongTy,
                               string PhuKien, string UuDai, int HangSX_ID,
                               string ThongSoKyThuat, string MoTaSanPham,
                               string HinhAnh, int Count_View, decimal GiaThiTruong, decimal GiaCongTy, DateTime NgayTao)
        {
            var role = new SanPham();
            var objEnt = da.SanPhams.Where(p => p.Ma_SP == Ma_SP).First();

            objEnt.Ten_SP = Ten_SP;
            objEnt.TrongLuong = TrongLuong;
            objEnt.HeDieuHanh = HeDieuHanh;
            objEnt.CPU = CPU;
            objEnt.OCung = OCung;
            objEnt.RAM = RAM;
            objEnt.Support_3G = Support_3G;
            objEnt.KetNoi3G = KetNoi3G;
            objEnt.Wifi = Wifi;
            objEnt.Camera = Camera;
            objEnt.TrangThaiKho = TrangThaiKho;
            objEnt.TinhTrang = TinhTrang;
            objEnt.BaoHanh = BaoHanh;
            objEnt.HangCongTy = HangCongTy;
            objEnt.PhuKien = PhuKien;
            objEnt.UuDai = UuDai;
            objEnt.HangSX_ID = HangSX_ID;
            objEnt.ThongSoKyThuat = ThongSoKyThuat;
            objEnt.MoTaSanPham = MoTaSanPham;
            objEnt.HinhAnh = HinhAnh;
            objEnt.Count_View = Count_View;
            objEnt.GiaThiTruong = GiaThiTruong;
            objEnt.GiaCongTy = GiaCongTy;
            objEnt.NgayTao = NgayTao;
            da.SaveChanges();
        }
        #endregion
        public void Create(SanPham role)
        {
            da.SanPhams.AddObject(role);
            da.SaveChanges();
        }


    }
}